package com.mc.lp.database.dao.impl;

import java.util.Collections;
import java.util.List;

import org.apache.commons.lang.math.NumberUtils;
import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Repository;

import com.mc.lp.database.dao.IRecommendationDAO;
import com.mc.lp.database.entity.RecommendationEntity;
import com.mc.lp.database.entity.type.RecordType;
import com.mc.lp.shared.vo.SearchCriteriaVO;

/*
 * @author aphanda
 * @date Dec 13, 2014
 * @time 9:00:00 PM
 */

@Repository
public class RecommendationDAOImpl extends AbstractGenericHibernateDAO<RecommendationEntity, Long, IRecommendationDAO> implements IRecommendationDAO {

	private static final long serialVersionUID = 1L;
	
	@SuppressWarnings("unchecked")
	@Override
	public List<RecommendationEntity> fetchRecommendationBySearchCriteria(SearchCriteriaVO vo) throws DataAccessException {

		List<RecommendationEntity> en = Collections.emptyList();
		Criteria crit = getCurrentSession().createCriteria(RecommendationEntity.class);
		if (vo.getTermId() > 0)
			crit.add(Restrictions.eq("termId", vo.getTermId()));
		if (vo.getGradeId() > 0)
			crit.add(Restrictions.eq("gradeId", vo.getGradeId()));
		
		if (vo.getStudentEntityId() != null && vo.getStudentEntityId() >0)
			crit.add(Restrictions.eq("studentEntity", vo.getStudentEntityId()));
		
		
		if (vo.getRecordType() != null)
			crit.add(Restrictions.eq("recordType", RecordType.getType(vo.getRecordType()).getId()));
		
		if (vo.getYear() != null)
			crit.add(Restrictions.eq("year", NumberUtils.toLong(vo.getYear())));

		en = (List<RecommendationEntity>) crit.list();

		return en;
	}
}
